package com.joehongfa.util;
//将xml转换为一个HashMap对象

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

public class XmlUtil {
	static Document document;

	private XmlUtil() {

	}

	public XmlUtil(String fileName) {
		document = DOMUtils.getXMLByFilePath(fileName);
	}

	public String getText(String tag) {
		Element e = getElement(document.getRootElement(), tag);
		return DOMUtils.getText(e);
	}

	private Element getElement(Element e, String nodeName) {
		Element result = DOMUtils.getChildElement(e, nodeName);

		if (result != null) {// 不为空说明在根节点下存在
			System.out.println("getElement result: " + result.getText());
			return result;
		}
		// 不存在则遍历根节点下的子节点
		Iterator iter = e.elementIterator();
		while (iter.hasNext()) {
			Element ele = (Element) iter.next();
			result = getElement(ele, nodeName);// 递归子节点
		}

		return null;
	}
}
